//
//  CPYSingerDetailViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/4/7.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "CPYSingerDetailViewController.h"
#import "CPYSingerDetailModel.h"
#import "CPYSingerDetailTableViewCell.h"
#import "CPYSingerInformationViewController.h"
@interface CPYSingerDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) TTMusicBaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation CPYSingerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self creatNavButtonItem];
    [self creatTableView];
    [self creatData];
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatTableView {
    self.tableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, SHORTHEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CPYSingerDetailTableViewCell class] forCellReuseIdentifier:@"CPYSingerDetailTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
}
- (void)creatData {
    [TTMusicAFNetworkTool GETNetWithUrl:[NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?size=1000&page=1&id=%@", self.detailID] body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            CPYSingerDetailModel *singerModel = [[CPYSingerDetailModel alloc] init];
            [singerModel setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:singerModel];
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_dataSource.count != 0) {
        return _dataSource.count;
    }else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataSource.count != 0) {
        CPYSingerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYSingerDetailTableViewCellIdentifier"];
        CPYSingerDetailModel *singerModel = _dataSource[indexPath.row];
        cell.singerDetailModel = singerModel;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80 * FitHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CPYSingerInformationViewController *informationVC = [[CPYSingerInformationViewController alloc] init];
    CPYSingerDetailModel *singerModel = _dataSource[indexPath.row];
    informationVC.singerID = [NSString stringWithFormat:@"%@", singerModel.singer_id];
    informationVC.imageStr = singerModel.pic_url;
    [self.navigationController pushViewController:informationVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
